<?php
	if (session_id() == '') {
    	session_start();
	}
	
	if(isset($_POST['address'])){
		require("konf.php");
		$yhendus=new mysqli($masin, $kasutaja, $parool, $baas);
		$q="INSERT INTO addres (isik_id, addres, asula_id, indeks, riik_id, soogikoht_id) VALUES (?,?,?,?,1,1)";
		$kask=$yhendus->prepare($q);
		$kask->bind_param("isis", $_SESSION['id'], mysql_real_escape_string($_POST['address']), $_POST['linn'], mysql_real_escape_string($_POST['indeks']));
		$kask->execute();
		$kask->close();
		$yhendus->close();
	}
	
	if(!isset($_SESSION["kasutaja"])){
		header('Location: ./login.php');
		exit;
	}
	
	if((isset($_POST['pw1'])and isset($_POST['pw2']))and $_POST['pw1']==$_POST['pw1']){
		require("konf.php");
		$yhendus=new mysqli($masin, $kasutaja, $parool, $baas);
		$pw=stripslashes($_POST['pw1']);
		$pw=mysql_real_escape_string($pw);
		$user_pw=$_SESSION['kasutaja']."_".$pw;
		$q="UPDATE isik SET parool=PASSWORD(?) WHERE isik_id=?";
		$kask=$yhendus->prepare($q);
		$kask->bind_param("si", $user_pw, $_SESSION['id']);
		$kask->execute();
		$kask->close();
		$yhendus->close();
		$v="Parool vahetatud";
		
	}
	
	
	if(isset($_SESSION["kasutaja"])){
		require("konf.php");
		$yhendus=new mysqli($masin, $kasutaja, $parool, $baas);
		
		$kask=$yhendus->prepare("SELECT distinct addres.addres, asula.nimetus, indeks, riik.nimetus FROM addres, isik, riik, asula 
		WHERE asula.asula_id=addres.asula_id and addres.riik_id=riik.riik_id and (addres.isik_id=?)");
		
		$yhendus->query("SET NAMES utf8");
		$kask->bind_param("i", $_SESSION["id"]);
		$kask->bind_result($address, $asula, $indeks, $riik);
		$kask->execute();
		$count=0;
		$addressid=array();
		while($kask->fetch()){
			$adr=new stdClass();
			$adr->address=$address;
			$adr->asula=$asula;
			$adr->indeks=$indeks;
			$adr->riik=$riik;
			array_push($addressid, $adr);
			$count++;
		}
		
		
		
		echo $yhendus->error;
		$yhendus->close();
		
	}

?>

<!DOCTYPE html>
<html>
<head>
<div id="wrapper">
<?php
require("pais.php")
?>
</head>

<body>
<div id="leftcolumn">

<?php
	if($count>0 and !isset($_POST['L_aadress']))
	{
		echo"<div id='left_leftcolumn'>
		<h1>Aadressid</h1>";

		foreach ($addressid as $adr) {
			
		echo"
		<table>
		<tr><td>$adr->address</td></tr>
		<tr><td>$adr->asula &nbsp; $adr->indeks</td></tr>
		<tr><td>$adr->riik</td></tr>
		</table>
		<hr>";
		}	
		if($count>1){
			echo"<br>";
		}
		echo"<form action=? method='post'>
		 <input type='submit', name='L_aadress', value='Lisa aadress'>
		 </form>";
		
		echo"<h1>Parooli vahetamine</h1>";
		if(isset($_POST['pw1']) and $_POST['pw1']!=$_POST['pw2']){
			echo"<h2 style='color:red;'>Parool ei kattu</h2>";
		}
		else {
			if(isset($v)){
				echo"$v";
			}
		}
		echo"<form action=? method='post'>
		<table>
		<tr><td>Uus parool:</td><td><input type='password' name='pw1' required></td></tr>
		<tr><td>Uuesti:</td><td><input type='password' name='pw2' required></td></tr>
		<tr><td><input type='submit' value='Vaheta parool'>
		</table>
		</form>
		
		<a href='?logout'>Logi välja</a></div>";

//////////////////Right column- tellimuste ajalugu.
		require_once 'ucp_abi.php';
		$arv = arv($_SESSION['id']);
		if($arv>0){
		echo"<div id='left_rightcolumn'><h2>Tellimuste ajalugu</h2>
		<table border=1>";
		$yhendus=new mysqli($masin, $kasutaja, $parool, $baas);
		$yhendus->query("SET NAMES utf8");
		$kask=$yhendus->prepare("SELECT tellimus_id, kuna, taidetud, kommentaar FROM tellimus WHERE isik_id=? ORDER BY kuna");
		$kask->bind_param("i", $_SESSION['id']);
		$kask->bind_result($t_id, $aeg, $taidetud, $kommentaar);
		$kask->execute();
		$a2=array();
		while($kask->fetch()){
			$tellimus=new stdClass();
			$tellimus->id=$t_id;
			$tellimus->aeg=$aeg;
			$tellimus->taidetud=$taidetud;
			$tellimus->kommentaar=$kommentaar;
			
			array_push($a2, $tellimus);
			
			}
			foreach ($a2 as $tel) {
				
					
				echo("<tr><th>Tellimuse NR</th><th>Aeg</th><th>Staatus</th><th>Kommentaar</th></tr>
				<tr><td>$tel->id</td><td>$tel->aeg</td><td>");
				if ($tel->taidetud==0){echo"Täitmisel";
				}else{
					echo"Tellimus täidetud";}
				echo"</td><td>$tel->kommentaar</td></tr>
				</tr><th></th><th>Toote nimi</th><th>kogus</th><th>tk hind</th><th>Kokku</th></tr>";
				$to=tooted($tel->id);
				foreach ($to as $toode) {
					echo"<tr><td></td>
					<td>$toode->nimetus</td>
					<td>$toode->kogus</td>
					<td>$toode->hind €</td>
					<td>".$toode->kogus*$toode->hind."€</td>
					</tr>";
				}
			}
				
						 
		echo"</table></div></div>";
		}
			
	}
	else if(isset($_POST['L_aadress'])){
		echo"<form action=? method='post'><table>
		<tr><td>Aadress: </td><td><input type='text' name='address' required></td></tr>
		<tr><td>Riik:</td><td><select name='riik'><option value='ee'>Eesti</option></select></td></tr>
		<tr><td>Asula: </td><td><select name='linn' required>";
		require ('ucp_abi.php');
		$linnad=riigid_linnad();
		foreach ($linnad as $linn) {
			echo"<option value='$linn->id'>$linn->linn</option>";
			
		}
		echo"</select></td></tr>
		<tr><td>Posti indeks:</td><td><input type='text' name='indeks' required></td><tr>
		<tr><td><input type='submit' value='Lisa' name='add'></td></tr>
		</form>
		</table>";
		
	}
	else {
		echo"Sul ei ole veel ühtegi aadressi, Kas soovid lisada ühe?";
		echo"<form action=? method='post'>
		<input type='submit', name='L_aadress', value='Lisa aadress'>
		</form>";
	}
	
	
	 	?>
</div>
<div id="rightcolumn">
<?php require("korv.php");?>

</div>
</div>
</body>
</html>     
